<template>
    <div class="login_view">
        <div class="head">
            <div class="container">
                <div class="left">
                    <div class="logo">
                        <span>{{$store.state.project_config.name.substring(0,2)}}</span>
                        {{$store.state.project_config.name.substring(2)}}
                    </div>
                </div>
            </div>
        </div>
        <div class="content">
            <div class="container">
                <div class="left">
                    <h2>
                        {{$store.state.project_config.name}}
                    </h2>
                </div>
                <div class="right">
                    <el-tabs v-model="active_name">
                        <el-tab-pane label="用户登陆" name="login">
                            <el-form ref="login_form" :model="login_form" label-width="80px">
                                <el-form-item 
                                    label="账号"
                                    prop="userId"
                                    :rules="[
                                        { required: true, message: '不能为空'}, 
                                    ]">
                                    <el-input v-model="login_form.userId"></el-input>
                                </el-form-item>
                                <el-form-item 
                                    label="密码"
                                    prop="password"
                                    :rules="[
                                        { required: true, message: '不能为空'}, 
                                    ]">
                                    <el-input v-model="login_form.password" show-password></el-input>
                                </el-form-item>
                            </el-form>
                            <div class="footer">
                                <el-button type="primary" @click="on_login">登陆</el-button>
                                <el-button>取消</el-button>
                            </div>
                        </el-tab-pane>
                        <el-tab-pane label="用户激活" name="register">
                            <el-form ref="register_form" :model="register_form" label-width="80px">
                                <el-form-item 
                                    label="账号"
                                    prop="userId"
                                    :rules="[
                                        { required: true, message: '不能为空'}, 
                                    ]">
                                    <el-input v-model="register_form.userId"></el-input>
                                </el-form-item>
                                <el-form-item 
                                    label="密码"
                                    prop="password"
                                    :rules="[
                                        { required: true, message: '不能为空'}, 
                                    ]">
                                    <el-input v-model="register_form.password" show-password></el-input>
                                </el-form-item>
                                <el-form-item 
                                    label="验证码"
                                    prop="verifyCode"
                                    :rules="[
                                        { required: true, message: '不能为空'}, 
                                    ]">
                                    <el-input v-model="register_form.verifyCode"></el-input>
                                </el-form-item>
                            </el-form>
                            <div class="footer">
                                <el-button type="primary" @click="on_register">立即激活</el-button>
                                <el-button>取消</el-button>
                            </div>
                        </el-tab-pane>
                    </el-tabs>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {  //登录视图组件
    name: 'Login_view',
    data(){
        return{
            login_form:{
                userId:'',
                password:'',
            }, //登陆表单
            register_form:{
                userId:'',
                password:'',
                verifyCode:'',
            }, // 注册表单
            active_name:'login', //当前标签页
        };
    },
    methods: {
        on_login(){ //登陆按钮点击事件
            this.$refs.login_form.validate((valid) => {
                if (valid) {
                    this.$store.commit('set_loading',{
                        state:true,
                        msg:'请稍后！',
                    });
                    this.$axios.json_post('/login',{  //登录
                        userId:this.login_form.userId,
                        password:this.login_form.password,
                    }).then(({data})=>{
                        this.$message({
                            message: '登陆成功！',
                            type: 'success'
                        });
                        this.$store.commit('set_user_data',{
                            name:data.userName,
                            userId:data.userId,
                        });
                        this.$router.push('/disk');
                    }).finally(()=>{
                        this.$store.commit('set_loading',{
                            state:false,
                        });
                    }).catch(()=>{
                        this.$message.error('登陆失败！');
                        this.login_form={};
                    });
                } else {
                    return false;
                }
            });
        },
        on_register(){ //注册按钮点击事件
            this.$refs.register_form.validate((valid) => {
                if (valid) {
                    this.$store.commit('set_loading',{
                        state:true,
                        msg:'请稍后！',
                    });
                    this.$axios.json_post('/register',{  //登录
                        userId:this.register_form.userId,
                        password:this.register_form.password,
                        verifyCode:this.register_form.verifyCode,
                    }).then(()=>{
                        this.$message({
                            message: '该账号已激活！',
                            type: 'success'
                        });
                    }).finally(()=>{
                        this.$store.commit('set_loading',{
                            state:false,
                        });
                    }).catch(()=>{
                        this.$message.error('激活失败！');
                        this.register_form={};
                    });
                } else {
                    return false;
                }
            });
        },
    },
}
</script>

<style scoped lang='scss'>
.login_view{
    width: 100%;
    height: 100vh;
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center 0;
    background-image: url(~@/assets/photos/login_bg.png);
    >.head{
        width: 100%;
        height: 75px;
        position: fixed;
        top: 0;
        >.container{
            width: 100%;
            max-width: 1000px;
            height: 100%;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            >.left{
                width: fit-content;
                >.logo{
                    height: 64px;
                    display: flex;
                    justify-content: flex-start;
                    align-items: center;
                    font-size:25px;
                    font-weight: bold;
                    letter-spacing: 2px;
                    line-height: 100%;
                    span{
                        margin-right: 2px;
                        background-color: #2b2e4a;
                        border-radius: 3px;
                        color: white;
                        padding: 5px 2px;
                    }
                }
            }
            >.right{
                display: flex;
                justify-content: flex-end;
                align-items: center;
                >.item{
                    font-size: 16px;
                    color: white;
                    line-height: 100%;
                    margin-left:50px;
                    border-radius: 3px;
                    letter-spacing: 2px;
                    cursor: pointer;
                }
            }
        }
    }
    >.content{
        width: 100%;
        min-width: 1000px;
        height: 100vh;
        overflow: hidden;
        display: flex;
        justify-content: center;
        align-items: center;
        >.container{
            width: 100%;
            height: fit-content;
            max-width: 1000px;
            margin: auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            text-align: right;
            /deep/.el-tabs__nav-wrap::after {
                background-color: #e4e7ed00;
            }
            /deep/.el-tabs__nav-scroll{
                margin-bottom: 20px;
            }
            >.left{
                font-size: 40px;
                color: white;
                letter-spacing: 5px;
            }
            >.right{
                background-color: #00000029;
                padding: 20px 40px;
                width: 440px;
                border-radius: 5px;
                backdrop-filter: blur(5px);
                .el-button{
                    padding: 10px 20px;
                }
            }
        }
    }
}
</style>
